#!/usr/bin/perl -w

# Written by Jay Varner 6/30/08, Atlanta, GA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;

use WWW::Mechanize;
use DateTime;

my @urls = (
          "http://mysite.net",
          "http://myothersite.org"
);

# Command to execute the webrestart shell script
my $fcgi_restart = "./webrestart";

# Command to restart Lighttpd (may vary by distro)
my $lighttpd_restart = "/etc/init.d/lighttpd restart";

# Set the current date and time for the log file using DateTime from CPAN
my $date_time = DateTime->now;

# Set path to the log file you want to use
my $log = '/path/to/log.txt';

# Loop through each of your sites
foreach my $site (@urls) {
     # Get the Status using WWW::Mechanize from CPAN
     my $mech = WWW::Mechanize->new();
     $mech->get( $site );
     my $status = $mech->status($site);
 
     # If there is a server error, we will restart all the FastCGI processes.
     if ($status == '500') {
          system $fcgi_restart;
     }
     # If the site was not found, we will restart Lighttpd.
     elsif ($status == '404') {
          system $lighttpd_restart;
     }
     # Open the log file and record the status of each site and the current time.
     open(DAT,">>$log") || die("Cannot Open File");
     print DAT "$site || $status || $date_time \n";
     close DAT;
}



